package Frame;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;

import Dao.DaoSqlData;
import Rule.NameTag;
import Table.MyTable;

@SuppressWarnings("serial")
public class ManagerSerchPanel extends JPanel{
	
	final JScrollPane scrollPane ;
	private JButton Button_serch;
	private JRadioButton RadioButton_Name;
	private JRadioButton RadioButton_No;
	private JTextField TextField_serch;
	 
	public ManagerSerchPanel(){
					
		RadioButton_Name = new JRadioButton(NameTag.common_Name,true);
		RadioButton_No = new JRadioButton(NameTag.common_No);
		ButtonGroup RadioButton_Group=new ButtonGroup();
		RadioButton_Group.add(RadioButton_Name);
		RadioButton_Group.add(RadioButton_No);
		TextField_serch = new JTextField("",8);		
		Button_serch = new JButton(NameTag.serch_1);
		Button_serch.addActionListener(new ButtonActionListener());		
		
		JPanel panel_1 = new JPanel();
		panel_1.add(RadioButton_Name);
		panel_1.add(RadioButton_No);
		panel_1.add(TextField_serch);
		panel_1.add(Button_serch);
			
		scrollPane=new JScrollPane();
		scrollPane.setBorder(BorderFactory.createTitledBorder(NameTag.common_Tittle));
		scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
		
		this.setLayout(new BorderLayout());	
		this.add(panel_1, BorderLayout.NORTH);
		this.add(scrollPane, BorderLayout.CENTER);	
		
	}
	
	private class ButtonActionListener  implements ActionListener {
			
		@SuppressWarnings({ "rawtypes", "unchecked" })
		@Override
		public void actionPerformed(ActionEvent e) {
					
	        Vector rows = null; 	        
			String sqlField_1;
			String sqlField_2 = TextField_serch.getText().trim();
			
			if(RadioButton_Name.isSelected()){
				sqlField_1 = "prd_Name";
			}else{
				sqlField_1 = "prd_No";
			}					
			try {
				ResultSet rs = DaoSqlData.getPrdtInfoByAsc(sqlField_1, sqlField_2);		
				if(! rs.isBeforeFirst()){
					JOptionPane.showMessageDialog(scrollPane, "查询不到任何数据！");
					TextField_serch.requestFocus();//获得焦点
					TextField_serch.selectAll();
					return;
				}
				rows = new Vector();		
				while(rs.next()){
					Vector currow = new Vector();
					currow.add(rs.getString(2));
					currow.add(rs.getInt(3));
					currow.add(rs.getFloat(4));
					rows.add(currow);
				}
			} catch (Exception e1) {
				
				e1.printStackTrace();
			}	 	
			MyTable mytable = MyTable.getTable(rows,true,false);	

		 	scrollPane.setViewportView(mytable);

		}	
	}
		
}
